package com.kaer.plugin.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.kaer.plugin.service.UpdateArmService;
import com.kaer.plugin.utils.JSONKeys;
import com.kaer.plugin.utils.SharedConfig;

/**
 * Created by lizj on 2016/11/15.
 */

public class PluginReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("PluginReceiver", "action " + intent.getAction());
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            context.startService(new Intent(context, UpdateArmService.class));
        } else if ("android.net.conn.CONNECTIVITY_CHANGE".equals(intent
                .getAction())) {
            ConnectivityManager manager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeInfo = manager.getActiveNetworkInfo();
            if (activeInfo != null) {
                long time = SharedConfig.getInstance(context).readlong(JSONKeys.Plugin.LAST_UPDATE, 0l);
                //没超过12小时就更新
                //if (System.currentTimeMillis() - time > 1000 * 60 * 60 * 12) {
                context.startService(new Intent(context, UpdateArmService.class));
                //  }
            }
        }

    }
}
